package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.eduservice.client.OrdersClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private OrdersClient ordersClient;

    //分页查询带条件查询课程
    @PostMapping("/getCourseFrontList/{page}/{limit}")
    public R  getCourseFrontList(@PathVariable long page, @PathVariable long limit,
                                 @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> coursePage=new Page<>(page,limit);
       Map<String,Object> map= eduCourseService.getCourseFrontList(coursePage,courseFrontVo);
        return R.ok().data(map);
    }

    //查询课程基本信息
    @GetMapping("/getBaseCourseInfo/{courseId}")
    public R getBaseCourseFrontInfo(@PathVariable String courseId, HttpServletRequest request){
        //编写sql语句
        //根据课程id查询详情信息
       CourseWebVo courseWebVo= eduCourseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoByCourseList = eduChapterService.getChapterVideoByCourse(courseId);
        //根据课程id和用户id查询课程是否已支付
        boolean buyCourse  = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        //
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoByCourseList",chapterVideoByCourseList).data("isBuy",buyCourse);
    }


    //根据课程id查询课程信息
    @PostMapping("/getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
